package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.MarketTopicRead;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/admin/topic/*")
public class AdminTopicServlet extends WdBaseServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String title = request.getParameter("title");
        String subtitle = request.getParameter("subtitle");
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        int page = Integer.parseInt(request.getParameter("page"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
         if(title !=null){
             criteria.andTitleLike("%"+title+"%");
         }
         if(subtitle !=null){
             criteria.andSubtitleLike("%"+subtitle+"%");
         }
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        MarketTopicMapper mapper = MyBatisUtil.getMapper(MarketTopicMapper.class);
        PageHelper.startPage(page, limit);

        List<MarketTopic> marketTopics = mapper.selectByExample(example);

//        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo(marketTopics);

        CommonData<MarketTopic> data = CommonData.data(new PageInfo(marketTopics));

//        ResponseUtil.responseOkJsonVo(response,marketTopics);
        ResponseUtil.responseJson(response, BaseRespVo.ok(data));


    }

    @SneakyThrows
    public void create(HttpServletRequest request, HttpServletResponse response) {
        String JsonStr = request.getReader().readLine();
        MarketTopic topic = JacksonUtil.read(JsonStr, MarketTopic.class);

        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topic.setDeleted(false);
        topic.setSortOrder(0);

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.insert(topic);
        sqlSession.commit();
        sqlSession.close();

        SqlSession sqlSession1 = TransactionUtil.getSqlSession();
        MarketTopicMapper mapper1 = sqlSession1.getMapper(MarketTopicMapper.class);
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andTitleEqualTo(topic.getTitle());
        List<MarketTopic> topics = mapper1.selectByExample(example);
        MarketTopic marketTopic = topics.get(0);
        sqlSession1.close();


        ResponseUtil.responseOkJsonVo(response, marketTopic);

    }

    public void read(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        MarketTopicRead marketTopicRead = new MarketTopicRead();

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper topicMapper = sqlSession.getMapper(MarketTopicMapper.class);
        MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);

        String[] goods = marketTopic.getGoods();
        List<MarketGoods> goodsList = marketTopicRead.getGoodsList();

        for (String good : goods) {
            int goodId = Integer.parseInt(good);
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodId);
            goodsList.add(marketGoods);
        }
        sqlSession.commit();
        sqlSession.close();
        marketTopicRead.setTopic(marketTopic);
        marketTopicRead.setGoodsList(goodsList);

        ResponseUtil.responseOkJsonVo(response, marketTopicRead);
    }


    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        MarketTopic topic = JacksonUtil.read(jsonStr, MarketTopic.class);

        Integer id = topic.getId();
        topic.setUpdateTime(new Date());

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.updateByPrimaryKey(topic);
        MarketTopic marketTopic = mapper.selectByPrimaryKey(id);

        sqlSession.commit();
        sqlSession.close();
        ResponseUtil.responseOkJsonVo(response, marketTopic);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        MarketTopic marketTopic = JacksonUtil.read(jsonStr, MarketTopic.class);
        Integer id = marketTopic.getId();

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.deleteByPrimaryKey(id);
        sqlSession.commit();
        sqlSession.close();

        ResponseUtil.responseOkJsonVo(response,null);
    }
}
